home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / DialogControls / Functions.c < prev    next >
C/C++ Source or Header  |  1995-01-26  |  4KB  |  210 lines

  1. #include "Functions.h"
  2.  
  3. /* Gets the ControlHandle for the item you want in the dialog box thebox.  */
  4. /* Handy for setting checkboxes and radio buttons */
  5. ControlHandle SnatchHandle(DialogPtr thebox, short theGetItem)
  6. {
  7.     short itemtype;
  8.     Rect itemrect;
  9.     Handle thandle;
  10.     
  11.     GetDialogItem(thebox, theGetItem, &itemtype, &thandle, &itemrect);
  12.     return((ControlHandle)thandle);
  13. } /* end SnatchHandle */
  14.  
  15. /** Sets the parameter text, but usually I only do one of them for dialog
  16.     boxes and stuff **/
  17. void    ParamOne( StringPtr string )
  18. {
  19.     ParamText( string, "\p", "\p", "\p" );
  20. }
  21.  
  22. /** Selects and unselects buttons based on the argument "which" **/
  23. void    SelectButton(DialogPtr dialog, int butID, Boolean which )
  24. {
  25.     ControlHandle    handle;
  26.     
  27.     handle = SnatchHandle( dialog, butID );
  28.  
  29.     if( handle )
  30.         SetControlValue( handle, (which?1:0) );
  31. }
  32.  
  33. /** Checks/Unchecks a button based on it's current state **/
  34. void    CheckButton( DialogPtr dialog, int butID )
  35. {
  36.     ControlHandle    handle;
  37.     
  38.     handle = SnatchHandle( dialog, butID );
  39.  
  40.     if( handle )
  41.         SetControlValue( handle, !(GetControlValue( handle ) ));
  42. }
  43.  
  44. /** Enables/Disables a button **/
  45. void    EnableButton( DialogPtr dialog, int butID, Boolean which )
  46. {
  47.     ControlHandle    handle;
  48.     short            how;
  49.     
  50.     if( which )
  51.         how = 0;
  52.     else
  53.         how = 255;
  54.         
  55.     handle = SnatchHandle( dialog, butID );
  56.     
  57.     if( handle )
  58.         HiliteControl( handle, how );
  59. }
  60.  
  61. /** Tells you whether a button is currently selected or not **/
  62. Boolean    ButtonIsSelected( DialogPtr dialog, int butID )
  63. {
  64.     ControlHandle        handle;
  65.     
  66.     handle = SnatchHandle( dialog, butID );
  67.     
  68.     if( handle )
  69.         return GetControlValue( handle );
  70.     else
  71.         return false;
  72. }
  73.  
  74. /** Gets the rect of an item in a dialog **/
  75. void    GetDialogItemRect( DialogPtr dialog, int ID, Rect *theRect )
  76. {
  77.    short itemtype;
  78.    Handle thandle;
  79.     
  80.    GetDialogItem(dialog, ID, &itemtype, &thandle, theRect);
  81. }
  82.  
  83. /** Draws a line around the rect of an item in a dialog **/
  84. void    FrameDialogItemRect( DialogPtr dialog, int ID )
  85. {
  86.     Rect    theRect;
  87.     short    itemtype;
  88.     Handle    theHandle;
  89.     
  90.     GetDialogItem( dialog, ID, &itemtype, &theHandle, &theRect );
  91.     FrameRect( &theRect );
  92. }
  93.  
  94. /** Enables/Disables menus **/
  95. void        MenuEnable( int  menuID, int  item, Boolean which )
  96. {
  97.     MenuHandle        menu;
  98.     
  99.     menu = GetMenuHandle( menuID );
  100.     
  101.     if( !menu )
  102.     {    
  103.         SysBeep( 10 );
  104.         return;
  105.     }
  106.     
  107.     if( which )
  108.         EnableItem ( menu, item );
  109.     else
  110.         DisableItem( menu, item );
  111. }
  112.  
  113. /** Checks/Unchecks Menus **/
  114. void     MenuCheck( int theMenu, short whichItem, Boolean which )
  115. {
  116.     MenuHandle        menu;
  117.     
  118.     menu = GetMenuHandle( theMenu );
  119.     if( !menu )
  120.     {
  121.         SysBeep( 10 );
  122.         ExitToShell();
  123.     }
  124.     
  125.     if( which )
  126.         SetItemMark( menu, whichItem, checkMark);
  127.     else
  128.         SetItemMark( menu, whichItem, noMark );
  129. }
  130.  
  131. /** Deletes menu items **/
  132. void    MyDeleteMenuItem( int menuID, int menuItem )
  133. {
  134.     MenuHandle        menu;
  135.     
  136.     menu = GetMenuHandle( menuID );
  137.     if( menu )
  138.     {
  139.         DeleteMenuItem( menu, menuItem );
  140.     }
  141. }
  142.  
  143. /** Gets a resource number from the name of that resource and the type **/
  144. short    ResourceNumberFromName( StringPtr name, ResType type )
  145. {
  146.     Handle        rHandle;
  147.     short        ID;
  148.     
  149.     SetResLoad( false );
  150.     rHandle = GetNamedResource( type, name );
  151.     if( !rHandle ) return 0;
  152.     GetResInfo( rHandle, &ID, &type, name );
  153.     SetResLoad( true );
  154.     return ID;
  155. }
  156.  
  157. /** Gets the resource name from the ID number **/
  158. void    GetResourceNameFromID( short ID, ResType type, StringPtr name )
  159. {
  160.     Handle        handle;
  161.     
  162.     SetResLoad( false );
  163.     
  164.     handle = GetResource( type, ID );
  165.     if( handle )
  166.     {
  167.         GetResInfo( handle, &ID, &type, name );
  168.     }
  169.     else
  170.     {
  171.         SetString( &name, "\p" );
  172.     }
  173. }
  174.  
  175. /** Returns the width of a rect **/
  176. int        RectWidth( Rect *rect )
  177. {
  178.     return (rect->right - rect->left );
  179. }
  180.  
  181. /** Returns the height of a rect **/
  182. int        RectHeight( Rect *rect )
  183. {
  184.     return (rect->bottom - rect->top );
  185. }
  186.  
  187. /** My own (very stupid) wait function **/
  188. pascal void    Wait( int ticks )
  189. {
  190.     long    oldTicks;
  191.     
  192.     oldTicks = TickCount();
  193.     
  194.     while( oldTicks > TickCount() - (long)ticks )
  195.         SystemTask();
  196. }
  197.  
  198. /** If you are handling key strokes, this is a good function
  199.     to flash a button to make the user think they pressed it **/
  200. void    FlashButton( DialogPtr dialog, int number )
  201. {
  202.     ControlHandle    handle;
  203.  
  204.     handle = SnatchHandle( dialog, number );
  205.     if( !handle ) return;
  206.  
  207.     HiliteControl( handle, kInButtonControlPart );
  208.     Wait( 4 );
  209.     HiliteControl( handle, 0 );
  210. }